package com.test.springCloud.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author luoxingfa
 * @createdate 2023/5/30 0030 17:10
 * @mood happy
 */
/**
 * 获取第三方接口数据
 * 一般写在service文件中，设置定时任务，定时获取第三方接口数据存入本服务器数据库
 */
@CrossOrigin  //跨域
@RequestMapping("/lxf")
@Slf4j
@RestController
public class RequesteApiController {
    /**
     * //获取第三方接口（get方法）
     * @return
     * @throws Exception
     */
    @RequestMapping("/get")
    @ResponseBody
    public String get()throws  Exception{
        //打开目标网址
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //发起请求
        HttpGet httpGet = new HttpGet("https://docker.easydoc.net/doc/81170005/cCewZWoN/N9VtYIIi");
        //请求头部信息,以下视情况而设置
        httpGet.addHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36");
        httpGet.addHeader("Accept", "*/*");
        httpGet.addHeader("Accept-Encoding", "gzip, deflate, br");
        httpGet.addHeader("Content-Type", "application/json");
//        httpGet.addHeader("Content-Type", "application/x-www-form-urlencoded");
        httpGet.addHeader("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8");
        httpGet.addHeader("Connection", "keep-alive");
        //设置请求参数，参数有可能需要加密处理
        HttpParams params = new BasicHttpParams();
        params.setParameter("userName","张三");
        params.setLongParameter("password",12341234);
        httpGet.setParams(params);
        //由httpclient发送请求
        CloseableHttpResponse response = httpClient.execute(httpGet);
        //解析请求
        if (response.getStatusLine().getStatusCode()==200){
            HttpEntity entity = response.getEntity();
            //将获取的数据进行解码处理
            String s = EntityUtils.toString(entity, "utf-8");
            //这一步一般都是选择存入数据库
            System.out.println(s);
        }
        return "";
    }

    /**
     * 获取第三方接口（post方法）
     * @return
     * @throws Exception
     */
    @RequestMapping("/post")
    @ResponseBody
    public String post()throws Exception{
        JSONObject js = new JSONObject();//用来返回数据信息
        //同样打开连接
        CloseableHttpClient httpclient = HttpClients.createDefault();
        //发送请求,这里是post请求，所以创建HttpPost
        HttpPost httpPost = new HttpPost("https://docker.easydoc.net/doc/81170005/cCewZWoN/N9VtYIIi");
        //设置请求头，请求头看要求设置
        httpPost.addHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36");
        httpPost.addHeader("Accept", "*/*");
        httpPost.addHeader("Accept-Encoding", "gzip, deflate, br");
        httpPost.addHeader("Content-Type", "application/json");
//        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
        httpPost.addHeader("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8");
        httpPost.addHeader("Connection", "keep-alive");
        //设置请求体,请求体一般都是json格式的数据
        JSONObject json = new JSONObject();
        json.put("userName","张三");//视情况设置多个

        httpPost.setEntity(new StringEntity(json.toString(),"utf-8"));
        //发送请求
        CloseableHttpResponse response = httpclient.execute(httpPost);
        //一般成功都返回200，具体看接口文档
        if (response.getStatusLine().getStatusCode()==200){
            HttpEntity entity = response.getEntity();
            //获取实体数据
            String s = EntityUtils.toString(entity, "utf-8");
            //添加进数据库或者其他操作
            /**
             * 数据库代码
             */
            System.out.println(s);
            js.put("code",200);
            js.put("msg","接口数据获取成功！");

            return  JSONObject.toJSONString(js, SerializerFeature.WriteMapNullValue);
        }else {
            js.put("code",500);
            js.put("msg","接口数据获取失败！");

            return  JSONObject.toJSONString(js, SerializerFeature.WriteMapNullValue);
        }

    }
}
